<template>
  <div
    v-if="visible"
    class="image-box-container"
    ref="elementRef"
    :style="{ top: state.top, height: state.height }"
  >
    <div class="image-box-title">
      相框
      <Icon class="image-box-icon" type="md-close" size="20" @click="handleClose" />
    </div>
    <div class="image-box-wrapper">
      <ul>
        <li v-for="item in state.data" :key="item.categoryId">
          <h4>
            {{ item.categoryName }}
            <span>查看全部</span>
          </h4>
          <ol>
            <li v-for="materia in item.materialList || []" :key="materia.id">
              <Image
                :src="materia.fileUrl"
                fit="contain"
                width="60px"
                height="60px"
                :alt="materia.fileName"
                :title="materia.fileName"
              />
            </li>
          </ol>
        </li>
      </ul>
    </div>
  </div>
</template>

<script name="imagebox" setup lang="ts">
import { ref, watch } from 'vue';
import { getImageBoxList } from '@/api/home';
import { to } from '@/utils/await-to';

const state = reactive({
  top: '10%',
  height: '80%',
  data: [],
});
const elementRef = ref(null);

const fetchImageBoxList = async () => {
  const [err, data] = await to(getImageBoxList());
  console.log(data);
  if (err) return;

  state.data = data?.data?.data || [];
};

defineProps(['visible']);
const emit = defineEmits(['handle-close']);

const handleClose = () => {
  emit('handle-close');
};

onMounted(() => {
  if (elementRef.value) {
    calculateDimensions(elementRef.value.offsetHeight);
  }

  fetchImageBoxList();
});

watch(elementRef, (el) => {
  if (el) {
    calculateDimensions(el.offsetHeight);
  }
});

const calculateDimensions = (height) => {
  const screenHeiht = window.innerHeight;
  state.top = (screenHeiht - height) / 2 + 'px';
};
</script>

<style lang="less" scoped>
.image-box-container {
  position: fixed;
  top: 0;
  left: 65px;
  width: 300px;

  border: 1px solid var(--color--border-float);
  background-color: var(--color--bg-float);
  box-shadow: var(--shadow-m);
  border-radius: 8px;
  padding: 6px;
  display: flex;
  flex-direction: column;

  .image-box-title {
    font-size: 12px;
    color: #999;
    margin: 6px 0;
    padding: 0 6px;
    display: flex;
    justify-content: space-between;

    .image-box-icon {
      color: var(--primary-color);
      cursor: pointer;

      &:hover {
        color: var(--primary-color-hover);
      }
    }
  }

  .image-box-wrapper {
    height: calc(100% - 65px);
    overflow-y: auto;

    > ul {
      > li {
        padding: 0 8px;

        > h4 {
          display: flex;
          align-items: center;
          justify-content: space-between;
          font-size: 12px;
          color: var(--color-text-block);
          padding: 6px 0px;

          span {
            color: var(--color-text-block);
            cursor: pointer;
          }
        }

        ol {
          display: flex;
          align-items: center;
          flex-wrap: wrap;

          li {
            width: 60px;
            height: 60px;
            overflow: hidden;
            margin-right: 6px;
            margin-bottom: 10px;
            cursor: pointer;
          }
        }
      }
    }
  }
}

.transition-box {
  margin-top: 32px;
  width: 100%;
  border-radius: 4px;
  background-color: #409eff;
  text-align: center;
  color: #fff;
  padding: 24px 0;
}
</style>
